/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

namespace DslEditorPowerToy.Recipes.Actions
{
    public class DeleteProjectItemsAction : ActionBase
    {
        private string _fileNames = string.Empty;
        private string _folderPath = string.Empty;
        private Project _referringProject = null;

        #region Constructors
        public DeleteProjectItemsAction()
            : base()
        {
        }
        #endregion

        #region Public Properties
        [Input(Required = true)]
        public string FileNames
        {
            get
            {
                return this._fileNames;
            }
            set
            {
                this._fileNames = value;
            }
        }
        [Input(Required = true)]
        public string FolderPath
        {
            get
            {
                return this._folderPath;
            }
            set
            {
                this._folderPath = value;
            }
        }
        [Input(Required = true)]
        [CLSCompliant(false)]
        public Project ReferringProject
        {
            get
            {
                return this._referringProject;
            }
            set
            {
                this._referringProject = value;
            }
        }
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Get list of names
            List<string> fileNames = this.BuildFilesNameList();
            
            //Navigate to folder
            string pathToFolder = Path.Combine(DteHelper.BuildPath(this._referringProject), 
                (string)this.Evaluate(this._folderPath));
            ProjectItem folder = DteHelper.FindItemByPath(DTE.Solution, pathToFolder);
            if ((null == folder)
                || (folder.Kind != Constants.vsProjectItemKindPhysicalFolder))
                return;

            //Remove files
            foreach (string fileName in fileNames)
            {
                ProjectItem file = DteHelper.FindItemByName(folder.ProjectItems, fileName, false);
                if (null != file)
                    file.Delete();
            }
        }

        public override void Undo()
        {
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._fileNames)
                throw new ArgumentNullException("FileNames");
            if (0 == this._fileNames.Length)
                throw new ArgumentOutOfRangeException("FileNames");
            if (null == this._folderPath)
                throw new ArgumentNullException("FolderPath");
            if (0 == this._folderPath.Length)
                throw new ArgumentOutOfRangeException("FolderPath");
            if (null == this._referringProject)
                throw new ArgumentNullException("ReferringProject");
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Build list of assembly names
        /// </summary>
        /// <returns></returns>
        private List<string> BuildFilesNameList()
        {
            List<string> list = new List<string>();
            list.AddRange(this._fileNames.Split(new char[] { ';' }));

            return list;
        }
        #endregion
    }
}
 
